package com.zhiliao.vms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiliao.vms.constant.ExceptConstant;
import com.zhiliao.vms.pojo.Admin;
import com.zhiliao.vms.pojo.vo.ResponseData;
import com.zhiliao.vms.services.AdminService;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 枫叶
 * @since 2020-10-21
 */
@ApiIgnore
@RestController
@ApiOperation("管理员用户管理")
@RequestMapping("/vms/admin")
public class AdminController {

    @Resource
    AdminService adminService;

    @Cacheable(value = "admin")
    @GetMapping("/selectOne")
    public ResponseData<Admin> selectOne(Admin admin) {
        if (admin == null) {
            return new ResponseData<>(ExceptConstant.VALID_EXCEPTION.getCode(), "异常的参数");
        }
        Admin data = adminService.selectOne(admin);
        if (data != null) {
            return new ResponseData<>(ExceptConstant.SUCCESS.getCode(), "查询成功", data);
        }
        return null;
    }

    @Cacheable(value = "admin")
    @GetMapping("/listByPram")
    public ResponseData<List<Admin>> selectList(Admin admin) {
        List<Admin> adminList = adminService.selectList(admin);
        if (adminList != null && adminList.size() > 0) {
            return new ResponseData<>(ExceptConstant.SUCCESS.getCode(), "查询成功", adminList);
        }
        return null;
    }

    /**
     * 查询用户列表，有分页
     *
     * @param index 当前页
     * @param size  每页大小
     * @return 结果集
     */
    @Cacheable(value = "admin")
    @GetMapping("/listPage/{index}/{size}")
    public ResponseData<List<Admin>> selectPage(@PathVariable(value = "index", required = true) int index
            , @PathVariable("size") Integer size) {
        Page<Admin> page = adminService.page(new Page<>(index <= 0 ? 1 : index, size <= 0 ? 10 : size));
        List<Admin> list = page.getRecords();
        return new ResponseData<>(ExceptConstant.SUCCESS.getCode(), ExceptConstant.SUCCESS.getMsg(), list);
    }

    @PostMapping("/addAdmin")
    public ResponseData<Long> addAdmin(@RequestBody(required = true) Admin admin) throws RuntimeException {
        Long adminId = adminService.addAdmin(admin);
        if (adminId == null) {
            //插入数据库失败
            return new ResponseData<>(ExceptConstant.UNKNOW_EXEPTION.getCode(), "写入数据失败！");
        }
        return new ResponseData<Long>(ExceptConstant.SUCCESS.getCode(), "添加成功！", adminId);
    }

    @CacheEvict(value = "admin" ,key = "#id")
    @DeleteMapping(value = "/deleteById/{id}")
    public ResponseData deleteAdmin(@PathVariable(required = true, value = "id") Long id) {
        if (adminService.deleteAdmin(id)) {
            return new ResponseData(ExceptConstant.SUCCESS.getCode(), "删除成功！");
        }
        return new ResponseData(ExceptConstant.NOTFOUNND.getCode(), "删除失败，未找到此资源");
    }

    @CachePut(value = "admin",key = "#admin.id")
    @PutMapping("/updateAdmin")
    public ResponseData updateAdmin(@RequestBody(required = true) Admin admin) {
        if (adminService.updateAdmin(admin)) {
            return new ResponseData(ExceptConstant.SUCCESS.getCode(), "更新成功！");
        }
        return new ResponseData(ExceptConstant.NOTFOUNND.getCode(), "更新失败,请确定资源是否存在。");
    }

}

